#!/usr/bin/python3
from abc import ABC, abstractmethod
import time
from utils import My_Util


class Base_Plugin(ABC):

    def __init__(self) -> None:
        self.enable = True
        self.processed = 0
        self.datasource = None
        self.start_timestamp = None
        self.end_timestamp = None

    def is_enable(self) -> bool:
        """
        是否启用
        """
        return self.enable

    def result(self) -> dict:
        """
        处理过的的任务数量，失败的任务数量，跳过的任务数量\n
        {
            "processed": 0
        }
        """
        return {"processed": self.processed}

    def depend_on_db(self) -> bool:
        """
        依赖数据库
        """
        return False

    def set_project_config(self, project_config):
        """
        设置工程配置
        """
        self.project_config = project_config

    def set_datasource(self, datasource):
        """
        设置数据源
        """
        self.datasource = datasource

    def get_datasource(self):
        """
        获取数据源
        """
        return self.datasource

    def start_record(self):
        """
        记录开始时间
        """
        self.start_timestamp = time.time()

    def end_record(self):
        """
        记录结束时间
        """
        self.end_timestamp = time.time()

    def error_log(self, *msg) -> str:
        """
        获取错误日志
        """
        return My_Util.get_color_text(" ".join(msg), 1)

    @abstractmethod
    def run(self, workspace: str, source: str) -> str:
        """
        运行脚本逻辑，并返回工程文件
        """
        return source

    def print(self, *msg):
        format_msg = f"""{self.__class__.__name__} -> {" ".join(msg)}"""
        print("\r", end="", flush=True)
        print(format_msg, end="", flush=True)
